package arquitetura.view;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

import exceptions.GeralException;

import arquitetura.fachada.Fachada;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AlterarSenhaPessoal extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5083288517383448983L;
	private JPasswordField pwdSenhaAtual;
	private JPasswordField pwdNovaSenha;
	private JPasswordField pwdConfirmaNovaSenha;
	private Fachada fachada;

	/**
	 * Launch the application.
	 */
	public static void exibeTela() {
		//EventQueue.invokeLater(new Runnable() {
			//public void run() {
				try {
					AlterarSenhaPessoal dialog = new AlterarSenhaPessoal();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setLocationRelativeTo(null);
					dialog.setModal(true);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			//}
		//});
	}

	/**
	 * Create the dialog.
	 */
	public AlterarSenhaPessoal() {
		fachada = Fachada.getInstancia();
		
		setTitle("Alterar Senha Pessoal");
		setBounds(100, 100, 251, 266);
		getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(240, 248, 255));
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(10, 11, 214, 171);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Informe a senha atual:");
		lblNewLabel.setBounds(10, 11, 110, 14);
		panel.add(lblNewLabel);
		
		pwdSenhaAtual = new JPasswordField();
		pwdSenhaAtual.setBounds(10, 30, 189, 20);
		panel.add(pwdSenhaAtual);
		
		JLabel lblInformeANova = new JLabel("Informe a nova senha:");
		lblInformeANova.setBounds(10, 65, 110, 14);
		panel.add(lblInformeANova);
		
		pwdNovaSenha = new JPasswordField();
		pwdNovaSenha.setBounds(10, 84, 189, 20);
		panel.add(pwdNovaSenha);
		
		JLabel lblConfirmeANova = new JLabel("Confirme a nova senha:");
		lblConfirmeANova.setBounds(10, 119, 151, 14);
		panel.add(lblConfirmeANova);
		
		pwdConfirmaNovaSenha = new JPasswordField();
		pwdConfirmaNovaSenha.setBounds(10, 138, 189, 20);
		panel.add(pwdConfirmaNovaSenha);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBounds(10, 186, 214, 36);
		getContentPane().add(panel_1);
		
		JButton button = new JButton("Salvar");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int codigoUsuarioLogado = Principal.getCodigoUsuarioLogado();
				String senhaAtual = String.valueOf(pwdSenhaAtual.getPassword());
				String novaSenha = String.valueOf(pwdNovaSenha.getPassword());
				String confirmacaoNovaSenha = String.valueOf(pwdConfirmaNovaSenha.getPassword());
				try {
					fachada.alterarSenhaPessoal(codigoUsuarioLogado, senhaAtual, novaSenha, confirmacaoNovaSenha);
					dispose();
				} catch (GeralException e1) {
					JOptionPane.showMessageDialog(null, "Ocorreu o seguinte erro: " + e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		button.setBounds(10, 7, 86, 23);
		panel_1.add(button);
		
		JButton button_1 = new JButton("Cancelar");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		button_1.setBounds(114, 7, 86, 23);
		panel_1.add(button_1);

	}
}
